package com.allwees.support.marketing.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.allwees.bs.core.model.constant.CacheName;
import com.allwees.core.common.constant.EDelete;
import com.allwees.core.common.constant.EStatus;
import com.allwees.support.marketing.dao.HotWordDao;
import com.allwees.support.marketing.domain.HotWord;
import com.allwees.support.marketing.dto.HotWordDto;
import com.allwees.support.marketing.service.HotWordService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Daniel
 * @Version 1.0.0
 * @Description
 * @CreateTime 2020/10/12 14:22
 */
@Service
public class HotWordServiceImpl extends ServiceImpl<HotWordDao,HotWord> implements HotWordService {

    private Page<HotWord> page = new Page<>(1,15,false);

    @Cacheable(cacheNames = CacheName.HOT_WORDS_PREFIX,key = "'top15'",sync = true)
    @Override
    public List<HotWordDto> first15() {
        return super.lambdaQuery()
                .select(HotWord::getWord,HotWord::getHighlight,HotWord::getSort)
                .eq(HotWord::getStatus,EStatus.NORMAL.value())
                .orderByDesc(HotWord::getSort)
                .page(page)
                .getRecords()
                .stream()
                .map(HotWordDto::new)
                .collect(Collectors.toList());
    }

    @Override
    @CacheEvict(cacheNames = CacheName.HOT_WORDS_PREFIX,allEntries = true)
    public boolean saveWord(HotWordDto dto) {
        return super.save(new HotWord(dto));
    }

    @Override
    @CacheEvict(cacheNames = CacheName.HOT_WORDS_PREFIX,allEntries = true)
    public boolean updateWord(HotWordDto dto) {
        return super.updateById(new HotWord(dto));
    }

    @Override
    @CacheEvict(cacheNames = CacheName.HOT_WORDS_PREFIX,allEntries = true)
    public boolean enable(Long id) {
        HotWord hotWord = new HotWord();
        hotWord.setId(id);
        hotWord.setStatus(EDelete.UNDELETED.value());
        return super.updateById(hotWord);
    }

    @Override
    @CacheEvict(cacheNames = CacheName.HOT_WORDS_PREFIX,allEntries = true)
    public boolean disable(Long id) {
        HotWord hotWord = new HotWord();
        hotWord.setId(id);
        hotWord.setStatus(EDelete.DELETED.value());
        return super.updateById(hotWord);
    }
}
